<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 给你一个包含 n 个整数的数组 nums和target，判断 nums 中是否存在三个元素 a，b，c ，使得 a + b + c = target ？请你找出所有和为 0 且不重复的三元组。
        function threeSum(nums) {
            let res = []
            nums.sort((a,b)=>a-b)
            for (let i=0;i<nums.length;i++){
                if (nums[i]===nums[i-1]) continue

                let left = i+1
                let right=nums.length-1
                
                while(left<right){
                    let sum = nums[i]+nums[left]+nums[right]
                    if(sum===0){
                        res.push([nums[i],nums[left],nums[right]])
                        while(left<right && nums[left]===nums[left+1]) left++
                        while(left<right && nums[right]===nums[right-1]) right--
                        left++
                        right--
                    }else if(sum>0){
                        right--
                    }else if(sum<0){
                        left++
                    }
                }
            }
            return res
        }
        console.log(threeSum([0,0,0,0]))
    </script>
</body>
</html>